package cn.edu.ujn.lab3.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.dao.UserMapper;
import cn.edu.ujn.lab3.service.UserService;
import cn.edu.ujn.lab3.util.Page;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    
    
    @Override
    public User findUser(String usercode, String password) {
        return userMapper.findUser(usercode, password);
    }


    @Override
    public Page<User> findUserList(Integer page, Integer rows, String userName, String userCode) {
        User user = new User();
        if(StringUtils.isNotBlank(userName)) {
            user.setUserName(userName);
        }
        if(StringUtils.isNotBlank(userCode)) {
            user.setUserCode(userCode);
        }
        // 当前页
        user.setStart((page-1) * rows) ;
        // 每页数
        user.setRows(rows);
        List<User> userList = userMapper.selectUserList(user);
        Integer count = userMapper.selectUserListCount(user);
        // 创建Page返回对象
        Page<User> result = new Page<>();
        result.setPage(page);
        result.setRows(userList);
        result.setSize(rows);
        result.setTotal(count);
        return result;
    }


    @Override
    public int create(User user) {
        return userMapper.insert(user);
    }


    @Override
    public User getById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }


    @Override
    public int update(User user) {
        return userMapper.updateByPrimaryKey(user);
    }


    @Override
    public int delete(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    
    
    
    
}
